home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / TransSkel.cpt / MSkelHelp.pas < prev    next >
Pascal/Delphi Source File  |  1987-01-07  |  7KB  |  259 lines

  1. {    TransSkel multiple-window demonstration: Help module}
  2.  
  3. {    This module handles a help window, in which text may be scrolled but}
  4. {    not edited.  A TextEdit record is used to hold the text, though.}
  5.  
  6. {    14 June 1986        Paul DuBois}
  7. {    7 January 1987    Ported to LightSpeed Pascal by Owen Hartnett            }
  8. {    Ωhm Software Company                                                        }
  9.  
  10. UNIT MSkelHelp;
  11.  
  12. INTERFACE
  13.  
  14.     USES
  15.         MultiSkelGlobs, common, TransSkelpas;
  16.  
  17.     PROCEDURE HelpWindInit;
  18.  
  19. IMPLEMENTATION
  20.  
  21.     VAR
  22.         teHelp : TEHandle;        { handle to help window TextEdit record }
  23.         helpScroll : ControlHandle;    { help window scroll bar }
  24.         helpLine, halfPage : integer;    { line currently at top of window }
  25.                                     { number of lines in half a window }
  26.  
  27.     PROCEDURE Halt;
  28.  
  29.     BEGIN
  30.         TEDispose(teHelp);
  31.         DisposeControl(helpScroll);
  32.         CloseWindow(helpWind);
  33.     END;
  34.  
  35. {    Scroll to the correct position.  lDelta is the}
  36. {    amount to CHANGE the current scroll setting by.}
  37.  
  38.     PROCEDURE DoScroll (lDelta : integer);
  39.  
  40.         VAR
  41.             newLine : integer;
  42.  
  43.     BEGIN
  44.         newLine := helpLine + lDelta;
  45.         IF newLine < 0 THEN
  46.             newLine := 0;
  47.         IF newline > GetCtlMax(helpScroll) THEN
  48.             newline := GetCtlMax(helpScroll);
  49.         SetCtlValue(helpScroll, newLine);
  50.         lDelta := (helpLine - newLine) * (teHelp^^.lineHeight);
  51.         TEScroll(0, lDelta, teHelp);
  52.         helpLine := newLine;
  53.     END;
  54.  
  55. {    Filter proc for tracking mousedown in scroll bar.  The part code}
  56. {    of the part originally hit is stored as the control's reference}
  57. {    value.}
  58.  
  59.     PROCEDURE TrackScroll (theScroll : ControlHandle;
  60.                                     partCode : integer);
  61.  
  62.         VAR
  63.             lDelta : integer;
  64.  
  65.     BEGIN
  66.         IF (partCode = GetCRefCon(theScroll)) THEN
  67.             BEGIN
  68.                 CASE partCode OF
  69.                     inUpButton : 
  70.                         lDelta := -1;
  71.                     inDownButton : 
  72.                         lDelta := 1;
  73.                     inPageUp : 
  74.                         lDelta := -halfPage;
  75.                     inPageDown : 
  76.                         lDelta := halfPage;
  77.                     OTHERWISE
  78.                         ;
  79.                 END;
  80.                 DoScroll(lDelta);
  81.             END;
  82.     END;
  83.  
  84. {    Handle hits in scroll bar}
  85.  
  86.     PROCEDURE Mouse (thePt : Point;
  87.                                     t : longint;
  88.                                     mods : integer);
  89.  
  90.         VAR
  91.             thePart, ignore : integer;
  92.  
  93.     BEGIN
  94.         thePart := TestControl(helpScroll, thePt);
  95.         IF thePart = inThumb THEN
  96.             BEGIN
  97.                 ignore := TrackControl(helpScroll, thePt, NIL);
  98.                 DoScroll(GetCtlValue(helpScroll) - helpline);
  99.             END
  100.         ELSE IF thePart <> 0 THEN
  101.             BEGIN
  102.                 SetCRefCon(helpScroll, longint(thePart));
  103.                 ignore := TrackControl(helpScroll, thePt, @TrackScroll);
  104.             END;
  105.     END;
  106.  
  107. {    Update help window.  The update event might be in response to a}
  108. {    window resizing.  If so, resize the rects and recalc the linestarts}
  109. {    of the text.  To resize the rects, only the right edge of the}
  110. {    destRect need be changed (the bottom is not used, and the left and}
  111. {    top should not be changed). The viewRect should be sized to the}
  112. {    screen.  Pull text down if necessary to fill window.}
  113.  
  114.     PROCEDURE update (resized : Boolean);
  115.  
  116.         VAR
  117.             r : Rect;
  118.             visLines, lHeight, topLines, nLines, scrollLines, ignore : integer;
  119.  
  120.     BEGIN
  121.         r := helpWind^.portRect;
  122.         EraseRect(r);
  123.         IF resized THEN
  124.             BEGIN
  125.                 r.left := r.left + 4;
  126.                 r.bottom := r.bottom - 2;
  127.                 r.top := r.top + 2;
  128.                 r.right := r.right - 19;
  129.  
  130.                 teHelp^^.destRect.right := r.right;
  131.                 teHelp^^.viewRect := r;
  132.                 TECalText(teHelp);
  133.                 lHeight := teHelp^^.lineHeight;
  134.                 nLines := teHelp^^.nLines;
  135.                 visLines := (r.bottom - r.top) DIV lHeight;
  136.                 halfPage := visLines DIV 2;
  137.                 topLines := (r.top - teHelp^^.destRect.top) DIV lHeight;
  138.                 scrollLines := visLines - (nLines - topLines);
  139.                 IF (scrollLines > 0) AND (topLines > 0) THEN
  140.                     BEGIN
  141.                         IF scrollLines > topLines THEN
  142.                             scrollLInes := topLines;
  143.                         TEScroll(0, scrollLines * lHeight, teHelp);
  144.                     END;
  145.                 scrollLines := nLines - visLines;
  146.                 helpLine := (r.top - teHelp^^.destRect.top) DIV lHeight;
  147.  
  148. {    move and resize the scroll bar as well.  The ValidRect call is done}
  149. {    because the HideControl adds the control bounds box to the update}
  150. {    region - which would generate another update event!  Since everything}
  151. {    gets redrawn below, the ValidRect is used to cancel the update.}
  152.  
  153.                 HideControl(helpScroll);
  154.                 r := helpWind^.portRect;
  155.                 r.left := r.right - 15;
  156.                 r.bottom := r.bottom - 14;
  157.                 r.top := r.top - 1;
  158.                 r.right := r.right + 1;
  159.                 SizeControl(helpScroll, r.right - r.left, r.bottom - r.top);
  160.                 MoveControl(helpScroll, r.left, r.top);
  161.                 IF nLines - visLines < 0 THEN
  162.                     ignore := 0
  163.                 ELSE
  164.                     ignore := nLines - vislines;
  165.                 SetCtlMax(helpScroll, ignore);
  166.                 SetCtlValue(helpScroll, helpLine);
  167.                 ShowControl(helpScroll);
  168.             END;
  169.         DrawGrowBox(helpWind);
  170.         DrawControls(helpWind);    { redraw scroll bar }
  171.         r := teHelp^^.viewRect;
  172.         TEUpdate(r, teHelp);        { redraw text display }
  173.         ValidRect(helpWind^.portRect);
  174.     END;
  175.  
  176. {    When the window comes active, disable the Edit menu and highlight}
  177. {    the scroll bar if there are any lines not visible in the content}
  178. {    region.  When the window is deactivated, enable the Edit menu and}
  179. {    un-highlight the scroll bar.}
  180.  
  181.     PROCEDURE Activate (active : Boolean);
  182.  
  183.         VAR
  184.             ignore : integer;
  185.  
  186.     BEGIN
  187.         DrawGrowBox(helpWind);
  188.         IF active THEN
  189.             BEGIN
  190.                 DisableItem(editMenu, 0);
  191.                 IF GetCtlMax(helpScroll) > 0 THEN
  192.                     ignore := 0
  193.                 ELSE
  194.                     ignore := 255;
  195.                 HiLiteControl(helpScroll, ignore);
  196.             END
  197.         ELSE
  198.             BEGIN
  199.                 EnableItem(editMenu, 0);
  200.                 HiLiteControl(helpScroll, 255);
  201.             END;
  202.         DrawMenuBar;
  203.     END;
  204.  
  205.     PROCEDURE HelpWindInit;
  206.  
  207.         VAR
  208.             r : Rect;
  209.             textHandle : Handle;
  210.             visLines, scrollLines : integer;
  211.  
  212.     BEGIN
  213.         helpWind := GetNewWindow(helpWindRes, NIL, WindowPtr(-1));
  214.         SkelWindow(helpWind, @Mouse, NIL, @Update, @Activate, NIL, @Halt, NIL, true);
  215.         TextFont(0);
  216.         TextSize(0);
  217.  
  218.         r := helpWind^.portRect;
  219.         r.left := r.left + 4;
  220.         r.bottom := r.bottom - 2;
  221.         r.top := r.top + 2;
  222.         r.right := r.right - 19;
  223.         teHelp := TENew(r, r);
  224.         textHandle := GetResource('TEXT', helpTextRes);        {read help text}
  225.         HLock(textHandle);
  226.         TEInsert(textHandle^, GetHandleSize(textHandle), teHelp);
  227.         HUnlock(textHandle);
  228.         ReleaseResource(textHandle);    { done with it, so goodbye }
  229.  
  230. {    Now figure out how many lines will fit in the window and how many}
  231. {    will not.  Determine the number of lines in half a window for use}
  232. {    in tracking clicks in the page up and page down regions of the}
  233. {    scroll bar.  Then create the scroll bar .  Make sure the borders }
  234. {    overlap the window frame and the frame of the grow box. }
  235.  
  236.         visLines := (r.bottom - r.top) DIV teHelp^^.lineHeight;
  237.         scrollLines := teHelp^^.nLines - visLines;
  238.         halfPage := visLines DIV 2;
  239.         helpline := 0;
  240.         r := helpWind^.portRect;
  241.         r.left := r.right - 15;
  242.         r.bottom := r.bottom - 14;
  243.         r.top := r.top - 1;
  244.         r.right := r.right + 1;
  245.  
  246. {    Build the scroll bar.  Don't need to bother testing whether to}
  247. {    highlight it or not, since that will be done in response to the}
  248. {    activate event.}
  249.  
  250.         helpScroll := NewControl(helpWind, r, '', true, helpLine, 0, scrollLines, scrollBarProc, 0);
  251.  
  252. {    GetNewWindow generates an update event for entire portRect.}
  253. {    Cancel it, since the everything has been drawn already,}
  254. {    except for the grow box (which will be drawn in response}
  255. {    to the activate event).}
  256.  
  257.         ValidRect(helpWind^.portRect);
  258.     END;
  259. END.